﻿using System.Windows.Input;

namespace software.Common.MVVM
{
    public class RelayCommand : CommandBase
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// </summary>
        /// <param name="execute">A delegate to execute when <see cref="ICommand.Execute(object)"/> is called on the command.</param>
        /// <param name="canExecute">A delegate to execute when <see cref="ICommand.CanExecute(object)"/> is called on the command.</param>
        /// <exception cref="ArgumentNullException"><paramref name="execute"/> is <c>null</c>.</exception>
        public RelayCommand(Action execute, Func<bool>? canExecute = null)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute ?? (() => true);
            CanExecuteChanged += RelayCommand_CanExecuteChanged;
        }

        private void RelayCommand_CanExecuteChanged(object? sender, EventArgs e)
        {
            CanExecute();
        }

        /// <inheritdoc cref="ICommand.CanExecute" />
        public bool CanExecute() => _canExecute();

        /// <inheritdoc cref="ICommand.Execute" />
        public void Execute()
        {
            if (_canExecute())
            {
                _execute();
            }
        }

        /// <inheritdoc />
        protected override bool CanExecute(object? parameter) => CanExecute();

        /// <inheritdoc />
        protected override void Execute(object? parameter) => Execute();
    }

    public class RelayCommand<T> : CommandBase
    {
        private readonly Action<T> _execute;
        private readonly Func<T, bool> _canExecute;

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand{T}"/> class.
        /// </summary>
        /// <param name="execute">A delegate to execute when <see cref="ICommand.Execute(object)"/> is called on the command.</param>
        /// <param name="canExecute">A delegate to execute when <see cref="ICommand.CanExecute(object)"/> is called on the command.</param>
        /// <exception cref="ArgumentNullException"><paramref name="execute"/> is <c>null</c>.</exception>
        public RelayCommand(Action<T> execute, Func<T, bool>? canExecute = null)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute ?? (_ => true);
        }

        public bool CanExecute(T? parameter) => parameter != null && _canExecute(parameter);

        public void Execute(T? parameter)
        {
            if (parameter != null && _canExecute(parameter))
            {
                _execute(parameter);
            }
        }

        /// <inheritdoc />
        protected override bool CanExecute(object? parameter) => CanExecute(CastParameter<T>(parameter));

        /// <inheritdoc />
        protected override void Execute(object? parameter) => Execute(CastParameter<T>(parameter));
    }
    public abstract class CommandBase : ObservableObject, ICommand
    {
        public event EventHandler? CanExecuteChanged
        {
            add => CommandManager.RequerySuggested += value;
            remove => CommandManager.RequerySuggested -= value;
        }


        /// <inheritdoc />
        bool ICommand.CanExecute(object? parameter) => CanExecute(parameter);

        /// <inheritdoc />
        void ICommand.Execute(object? parameter) => Execute(parameter);

        /// <summary>
        /// A helper method to typecast the command parameter.
        /// </summary>
        /// <param name="parameter">The data used by the command.</param>
        /// <typeparam name="T">The command parameter type.</typeparam>
        /// <returns>A converted value; or default value if <paramref name="parameter"/> is <c>null</c>.</returns>
        protected static T? CastParameter<T>(object? parameter) => parameter == null ? default : (T)parameter;

        /// <summary>
        /// When implemented by a derived class, invokes the command's <see cref="ICommand.CanExecute"/> handler.
        /// </summary>
        /// <param name="parameter">The data used by the command.</param>
        /// <returns><c>true</c> if the command can be executed; otherwise, <c>false</c>.</returns>
        protected abstract bool CanExecute(object? parameter);

        /// <summary>
        /// When implemented by a derived class, invokes the command's <see cref="ICommand.Execute"/> handler.
        /// </summary>
        /// <param name="parameter">The data used by the command.</param>
        protected abstract void Execute(object? parameter);
    }

}